<?php
/**
 *商品库存调拨申请模型
 */
class Cron_Model_Backup_StockTransferApply extends Application_Model_StoryTransferApply {
	private $_transferWarehouseModel;
	private $_storyModel;

	/**
	 * 需求调拨申请
	 * @param  [type]           $oneStory  一条需求记录(b_story表)
	 * @return [type]           [description]
	 */
	public function apply(array $oneBackupStory) {
		$this->_transferWarehouseModel || $this->_transferWarehouseModel = new Cron_Model_Backup_TransferWarehouse();
		$this->_storyModel || $this->_storyModel = new Application_Model_Story();
		//需求需要调拨的数量是a,c需求之和，如果它小于零，表示不用调拨
		$storyApplyQuantity = $oneBackupStory['a_story_quantity'] + $oneBackupStory['c_story_quantity'];
		if ($storyApplyQuantity <= 0) {
			return true;
		}

		$goodsSn = $oneBackupStory['goods_sn'];
		$transferToWarehouse = $oneBackupStory['warehouse_code'];

		$transferFromWarehouse = $this->_transferWarehouseModel->getTransferFromWarehouse($transferToWarehouse, $goodsSn);
		if ($transferFromWarehouse === false) {
			return true;
		}

		$warehouseCanTransferNum = $this->_transferWarehouseModel->allowTransferQuantity($goodsSn, $transferFromWarehouse, $transferToWarehouse);
		if ($warehouseCanTransferNum <= 0) {
			return true;
		}

		$applyData = [
			'story_id' => $oneBackupStory['id'],
			'goods_sn' => $goodsSn,
			'from_warehouse' => $transferFromWarehouse,
			'to_warehouse' => $transferToWarehouse,
			'apply_quantity' => $storyApplyQuantity,
			'can_transfer_quantity' => $warehouseCanTransferNum,
			'create_date' => date("Y-m-d"),
			'status' => '待审核',
		];

		$this->writeDb->beginTransaction();
		try {
			$this->writeDb->insert("b_stock_transfer_apply", $applyData);
			$transferApplyId = $this->writeDb->lastInsertId();

			$transferLogData = [
				'story_id' => $oneBackupStory['id'],
				'stock_transfer_apply_id' => $transferApplyId,
				'before_transfer_a_story_quantity' => $oneBackupStory['a_story_quantity'],
				'before_transfer_b_story_quantity' => $oneBackupStory['b_story_quantity'],
				'before_transfer_c_story_quantity' => $oneBackupStory['c_story_quantity'],
			];

			$this->writeDb->insert('b_story_transfer_log', $transferLogData);
			$this->writeDb->update("b_story", ['is_apply_stock_transfer' => 1], 'id=' . $oneBackupStory['id']);
			$this->writeDb->commit();
			return true;
		} catch (Exception $e) {
			$this->writeDb->rollBack();
			return $this->_error($e->getMessage());
		}
	}

	/**
	 * 需求调拨可以申请的数量
	 * @return [type] [description]
	 */
	public function canApplyQuantity($goodsSn, $transferFromWarehouse, $transferToWarehouse) {
		$transferApply = $this->getOne($goodsSn, $transferFromWarehouse, $transferToWarehouse, null, true);
		if ($transferApply) {
			$canTransferQuantity = min($transferApply['can_transfer_quantity'], $transferApply['apply_quantity']);
		} else {
			$canTransferQuantity = 0;
		}
		return $canTransferQuantity;
	}

	/**
	 * 系统审核通过申请
	 * @return [type] [description]
	 */
	public function agreeBySystem($transferApplyId) {
		return $this->_agree($transferApplyId, self::SYSTEM_AGREE);
	}

	/**
	 * 调拨申请系统驳回
	 * @return [type] [description]
	 */
	public function rejectBySystem($transferApplyId) {
		return $this->_reject($transferApplyId, self::SYSTEM_REJECT);
	}

	/**
	 * 获取待审核列表
	 * @return [type] [description]
	 */
	public function getWaitCheckApply() {
		return $this->_getApplyByStatus('待审核');
	}

}